let express = require("express")();
const { request, response } = require("express");
let expressStatic = require("express-static");
let mysql = require("mysql");
const port = 82;
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test',
    timezone: "08:00"
});
sql.connect();
//得到数据
express.get("/getStudent", (request, response) => {
        sql.query(`SELECT * FROM student`, (error, data) => {
            if (error) {
                response.send("error");
                console.log(error)
            } else {
                response.send(JSON.stringify(data))
            }
        })
    })
    // sql.query(`SELECT * FROM student`, (error, data) => {
    //     console.log(data)
    // })
    //删除
express.get("/deleteStudent", (request, response) => {
        sql.query(`DELETE FROM student WHERE id=${request.query.id}`, (error, data) => {
            if (error) {
                response.send("error");
                console.log(error)
            } else {
                response.send("success")
            }
        })
    })
    //增加
express.get("/addStudent", (request, response) => {
        sql.query(`INSERT INTO student (name,age,sex,city,joinDate) VALUES ("${request.query.name}","${request.query.age}","${request.query.sex}","${request.query.city}","${request.query.joinDate}")`, (error, data) => {
            if (error) {
                response.send("error");
                console.log(error)
            } else {
                response.send("success")
            }
        })
    })
    //更新
express.get("/updateStudent", (request, response) => {
        sql.query(`UPDATE student SET name="${request.query.name}",age="${request.query.age}",sex="${request.query.sex}", city="${request.query.city}",joinDate="${request.query.joinDate}" WHERE id="${request.query.id}"`, (error, data) => {
            if (error) {
                response.send("error");
                console.log(error)
            } else {
                response.send("success")
            }
        })
    })
    //登录页面
express.get("/login", (request, response) => {
        sql.query(`SELECT * FROM users WHERE username="${request.query.username}" AND password="${request.query.password}"`, (error, data) => {
            if (error) {
                response.send("3");
                console.log(error)
            } else {
                if (data.length === 0) {
                    response.send("2")
                }
                response.send(JSON.stringify(data))
                console.log(data)
            }
        })
    })
    //注册页面
express.get("/addUser", (request, response) => {
    sql.query(`INSERT INTO users(name,username,password,usertype) VALUES ("${request.query.name}","${request.query.username}","${request.query.password}","${request.query.usertype}")`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send("success")
            console.log(`${request.query.username}用户注册成功`)

        }
    })
})

express.use(expressStatic(__dirname + '/static'))
console.log(`running at ${port}`)
express.listen(port)